<?php
/**
 * 描述：船配-产品列表。
 * @final
 */
final class Exec_Ancillary_Supply_List extends Exec
{
    public function Get()
    {
        #绑定数据-经营模式
        $data=array
        (
            array('Name'=>'生产加工','Value'=>'PRODUCTION'),
            array('Name'=>'经销批发','Value'=>'WHOLESALE'),
            array('Name'=>'招商代理','Value'=>'BUSINESS'),
            array('Name'=>'商业服务','Value'=>'SERVICE')
        );
        View::Assign('Shipyard_Index_BusinessModel',$data);
        
        #获取参数
        $type=Functions::UrlArgs('type');

        #定义标量
        $contentTitle=NULL;
        $data=NULL;
        $params=array('type'=>$type);

        #执行动作
        switch($type)
        {
            case 'keywords':
                $keywords=Functions::UrlArgs('keywords');
                $data=$this->Get_Keywords($keywords);
                $contentTitle=$keywords;
            break;

            case 'supermarket':
                $data=$this->Get_Supermarket();
                $contentTitle='产品超市';
            break;

            case 'class':
                $classID=Functions::UrlArgs('id');
                if(!is_numeric($classID))
                {
                    echo 'error';
                    return;
                }
                $data=$this->Get_Class($classID);
                $params['id']=$classID;
                $className=NULL;
                {
                    $tmp=DB::GetRowByID('category','Identifying:'.$classID);
                    if($tmp){
                       $contentTitle=$tmp['Name'];
                    }
                }
                $this->Sub_Class($classID,'class');
            break;

            case 'subclass':
                $subclassID=Functions::UrlArgs('id');
                if(!is_numeric($subclassID))
                {
                    echo 'error';
                    return;
                }
                $data=$this->Get_Subclass($subclassID);
                $params['id']=$subclassID;
                $className=NULL;
                {
                    $tmp=DB::GetRowByID('category','Identifying:'.$subclassID);
                    if($tmp){
                       $contentTitle=$tmp['Name'];
                    }
                }
                $this->Sub_Class($subclassID,'subclass');
            break;

            case 'search':
                $contentTitle='搜索产品信息';
                $province=trim(@$_GET['province']);
                $city=trim(@$_GET['city']);
                $year=trim(@$_GET['year']);
                $title=trim(@$_GET['title']);

                $sqlParams=array();
                $sql='select * from supply where 1=1 ';
                {
                    if(is_numeric($province)&&(int)$province!=-1)
                    {
                        $sql=$sql.'and Account in(select Username from account where Province=?) ';
                        $sqlParams[]=$province;
                        $params['province']=$province;
                    }
                    
                    if(is_numeric($city)&&(int)$city!=-1)
                    {
                        $sql=$sql.'and Account in(select Username from account where City=?) ';
                        $sqlParams[]=$city;
                        $params['city']=$city;
                    }

                    if(is_numeric($year)&&(int)$year!=-1)
                    {
                        $sql=$sql.'and year(Datetime)=? ';
                        $sqlParams[]=$year;
                        $params['year']=$year;
                    }

                    if($title!=''&&$title!=NULL)
                    {
                        $sql=$sql.'and Title like ? ';
                        $sqlParams[]='%'.$title.'%';
                        $params['title']=$title;
                    }
                }
                $sql=$sql.'order by ID desc limit ?,10';
                $data=DB::PGetData($sql,$sqlParams);
            break;

            case 'recommend':
                $contentTitle='精品推荐';
                $data=DB::GetData('select * from supply where M_Recommend_Is=TRUE order by M_Recommend_Datetime desc,ID desc limit ?,20');
            break;

            case 'latest':
                $data=$this->Get_Latest();
                $contentTitle='最新供应产品信息';
            break;
        }

        #数据处理
        foreach($data['Record'] as &$row)
        {
            //获取企业数据
            $tmp=DB::PGetRows('select * from account where Username=?',array(
                $row['Account']
            ));
            if($tmp)
            {
                $row['Account']=$tmp[0];
                //是否VIP
                $tmp=DB::PGetRows('select * from account_vip where Account=?',array(
                    $row['Account']['Username']
                ));
                if($tmp){
                    $row['Account']['IsVip']='TRUE';
                }
                else{
                    $row['Account']['IsVip']='FALSE';
                }
                //类别
                $row['ClassName']=NULL;
                {
                    $tmp=DB::GetRowByID('category','Identifying:'.$row['ClassID']);
                    if($tmp){
                        $row['ClassName']=$tmp['Name'];
                    }
                }
                $row['SubclassName']=NULL;
                {
                    $tmp=DB::GetRowByID('category','Identifying:'.$row['SubclassID']);
                    if($tmp){
                        $row['SubclassName']=$tmp['Name'];
                    }
                }
            }
            //处理图片路径
            $row['ThumbnailPath']=Config::PATH_RELATIVE_ATTACHMENT_SUPPLY.'/'.$row['ThumbnailPath'];
        }

        #渲染输出
        View::Assign('ContentTitle',$contentTitle);
        View::Assign('Supplies',$data['Record']);
        View::Assign('Params',$params);
        View::Assign('PageMenu',PagerFactory::Create('Common')->Fetch($data,array
        (
            'URL'=>'/exec.php?action=ancillary_supply_list',
            'IsDetail'=>TRUE,
            'Parameters'=>$params,
            'Rewriter'=>($type=='search') ? NULL:new PagerRewrite('/supply/','type,id','html')
        )));
        View::Display('ancillary/supply/list.tpl');
    }

    /**
     * 描述：子过程-绑定类别数据。
     * @access private
     */
    private function Sub_Class($classid,$type)
    {
        #定义标量
        $fieldName=($type=='class') ? 'ClassID':'SubclassID';

        #SQL语句
        $sql='select * from category where Identifying_Parent='.$classid.' order by ID asc';
        if($type=='subclass')
        {
            $tmp=DB::GetRowByID('category','Identifying:'.$classid);
            if($tmp){
                $sql='select * from category where Identifying_Parent='.$tmp['Identifying_Parent'].' order by ID asc';
            }
        }
        #获取数据
        $data=DB::GetRows($sql);
        if($data)
        {
            foreach($data as &$row)
            {
                $row['SupplyCount']=0;
                $tmp=DB::GetRows('select count(*) as SupplyCount from supply where SubclassID='.$row['Identifying']);
                if($tmp){
                    $row['SupplyCount']=$tmp[0]['SupplyCount'];
                }
            }
        }

        #绑定模板
        View::Assign('Class',$data);
    }

    private function Get_Latest()
    {
        $data=DB::GetData('select * from supply order by Datetime desc limit ?,20');
        return $data;
    }

    /**
     * 描述：按大类获取数据。
     * @access private
     */
    private function Get_Class($classID)
    {
        #获取数据
        $data=DB::GetData('select * from supply where ClassID='.$classID.' order by Datetime desc limit ?,20');
        return $data;
    }

    /**
     * 描述：按小类获取数据。
     * @access private
     */
    private function Get_Subclass($subclassID)
    {
        #获取数据
        $data=DB::GetData('select * from supply where SubclassID='.$subclassID.'  order by Datetime desc limit ?,20');
        return $data;
    }

    /**
     * 描述：按产品超市获取数据。
     * @access private
     */
    private function Get_Supermarket()
    {
        #获取数据
        $data=DB::GetData('select * from supply where M_Supermarket_Is=true order by M_Supermarket_Top_Is desc,M_Supermarket_Top_DateTime desc,Datetime desc limit ?,20');
        return $data;
    }

    /**
     * 描述：按关键字获取数据。
     * @access private
     */
    public function Get_Keywords($keywords)
    {
        #获取数据
        $data=DB::PGetData("select * from supply where Title like ? order by Datetime desc limit ?,20",array(
            '%'.$keywords.'%'
        ));
        return $data;
    }
}
?>